Skip to content

feat: added support for sort-imports-ignore#237

Merged
byara merged 6 commits into
trivago:mainfrom
stephdotnet:sort-imports-ignored
Oct 24, 2023
Merged

feat: added support for sort-imports-ignore#237
byara merged 6 commits into
trivago:mainfrom
stephdotnet:sort-imports-ignored

Conversation

@stephdotnet
Copy link
Copy Markdown
Contributor

Hi,

Thank you for this amazing package. I have been using it on some of my projects. However, recently I needed to ignore the import order for certain specific files.

I encountered this issue: #230 and decided to contribute to the solution.

I chose to follow the basic idea of ignoring the file if a leading comment // sort-imports-ignore is detected (as you mentioned in a previous issue, it is tricky to ignore specific lines, and we will probably need an RFC before diving into it: #26).

I attempted to follow your approach for parsing and formatting the code, which was very interesting by the way, and added the necessary tests.

Please feel free to ask for more if needed, and I will make the appropriate changes.

@ayusharma ayusharma requested review from ayusharma and byara and removed request for byara June 26, 2023 09:41
@d0whc3r
Copy link
Copy Markdown

d0whc3r commented Jun 27, 2023

maybe the string to search for ignore could be in the "constants.ts" file

@stephdotnet
Copy link
Copy Markdown
Contributor Author

maybe the string to search for ignore could be in the "constants.ts" file

I actually didn't notice the constants.ts file and i'll add that right the way. Nice catch @d0whc3r

@codemers
Copy link
Copy Markdown

Would really like this feature to! Thanks @stephdotnet

@bdombro
Copy link
Copy Markdown

bdombro commented Oct 23, 2023

@ayusharma could you please review this? Seems to be a popular feature request.

@ayusharma ayusharma requested a review from byara October 23, 2023 18:48
Copy link
Copy Markdown
Collaborator

@ayusharma ayusharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me ❤️ @byara can you please check 🙏

@byara byara merged commit c8bb4fa into trivago:main Oct 24, 2023
@byara
Copy link
Copy Markdown
Collaborator

byara commented Nov 10, 2023

Released in v4.3.0

@danielR2001
Copy link
Copy Markdown

danielR2001 commented Nov 18, 2023

@byara I did add // sort-imports-ignore but it still sorts the imports on save (I have prettier on save) 😞
Do I need something else?
this is my prettierrc

{
  "plugins": [
    "@trivago/prettier-plugin-sort-imports"
  ],
  "importOrder": [
    "^@(?!library1|library2)",
    "^[./]"
  ],
  "importOrderSeparation": true,
  "importOrderSortSpecifiers": true,
  "importOrderParserPlugins": [
    "typescript",
    "decorators-legacy"
  ]
}

@TheMikeyRoss
Copy link
Copy Markdown

@byara I did add // sort-imports-ignore but it still sorts the imports on save (I have prettier on save) 😞 Do I need something else? this is my prettierrc

{
  "plugins": [
    "@trivago/prettier-plugin-sort-imports"
  ],
  "importOrder": [
    "^@(?!library1|library2)",
    "^[./]"
  ],
  "importOrderSeparation": true,
  "importOrderSortSpecifiers": true,
  "importOrderParserPlugins": [
    "typescript",
    "decorators-legacy"
  ]
}

Same for me as well,

@danielR2001 Did you manage to resolve it?

@zaaakher
Copy link
Copy Markdown

zaaakher commented Dec 9, 2023

@TheMikeyRoss @danielR2001 Make sure // sort-imports-ignore is at the very top of the file

@TheMikeyRoss
Copy link
Copy Markdown

@TheMikeyRoss Make sure // sort-imports-ignore is at the very top of the file

Thanks @zaaakher that fixed it for me

@danielR2001
Copy link
Copy Markdown

@TheMikeyRoss @danielR2001 Make sure // sort-imports-ignore is at the very top of the file

Thanks @zaaakher , that fixed for me too!

@qiutian00
Copy link
Copy Markdown

@TheMikeyRoss @danielR2001 Make sure // sort-imports-ignore is at the very top of the file

This works for me.

@farzadmf
Copy link
Copy Markdown

farzadmf commented May 2, 2024

Hey everyone. Is this also supported in jsx files? I have:

{
  "endOfLine": "auto",
  "importOrder": ["^@/(.*)$", "^[./]"],
  "importOrderParserPlugins": ["typescript", "jsx", "decorators-legacy"],
  "importOrderSeparation": true,
  "importOrderSortSpecifiers": true,
  "plugins": ["@trivago/prettier-plugin-sort-imports"],
  "printWidth": 120,
  "proseWrap": "always",
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "all"
}

But // sort-imports-ignore doesn't stop anything (even when placed at the top of the file)

EDIT: technically, it's a .tsx file that I'm running prettier on

@farzadmf
Copy link
Copy Markdown

farzadmf commented May 2, 2024

To add to what I wrote above, my "interesting lines" that I don't want to move are for jest:

const myMock = jest.fn();

jest.mock('src/dependency/imported/by/MyModule', () => { /* ... */ });

// import other stuff
// import MyModule (jest.mock call would capture the dpendency here)

// My tests

Now, when imports are sorted, obviously, jest.mock will have no effect. Is there any solution/workaround for this? (I don't want to add the test files to .prettierignore because then I lose all the formatting functionality)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.